jQ(function(){
	jQ("a.search_friend").addClass("search_friendonit");
	fHideFocus("BUTTON");
	jQ("#search-passagetime li.content").click(function(){
		jQ("#search-passagetime li.content").removeClass("onit");
		jQ(this).addClass("onit");
		var effectid=jQ(this).attr("effectzone");
		var type=jQ(this).attr("loadtype");
		jQ("div.form").hide();
		jQ("#"+effectid).show();
		jQ("#"+effectid+" input.loadtype").val(type);
	});
	analyzeURL();
});

function analyzeURL(){
	var searchcontent=getParameter("content");
	if(searchcontent==null||searchcontent.length==0||typeof(searchcontent)=='undefined'){
		var usertype=getParameter("usertype");
		if(usertype==null||usertype.length==0||typeof(usertype)=='undefined'){
			
		}else{
			jQ("#search_type_sel").val(usertype);
		}
	}else{
		jQ("#searchinput").removeClass("searchdefault").addClass("searchonit");
		jQ("#searchinput").val(decodeURI(searchcontent));
		var usertype=getParameter("usertype");
		if(usertype==null||usertype.length==0||typeof(usertype)=='undefined'){
			
		}else{
			jQ("#search_type_sel").val(usertype);
		}
		searchUser(jQ("#searchbutton"));
	}
}

function fHideFocus(tName){
   jQ(tName).focus(function(){
	   jQ(this).blur();
   })
}

function chooseschool(){
	
}


function showAddContact(event){
	jQ("li.requestzone").hide().empty();
	var zone=jQ(event.currentTarget).parent().prev("li.requestzone");
	zone.animate({ 
	    height:"110px"
	  }, 800,function(){
		  jQ(this).append(jQ("#requestfriendzoneout").html()).fadeIn();
		  jQ(event.currentTarget).hide();
		  var uid=jQ(event.currentTarget).prev("input.uid").val();
		  jQ("#requestfrienduid").val(uid);
		  jQ("#requestfriend-confirm").unbind("click").bind("click",{optobj:jQ(event.currentTarget)},addContact);
		  jQ("#requestfriend-cancel").unbind("click").bind("click",function(){
			  jQ("li.requestzone").hide().empty();
			  jQ(event.currentTarget).show();
		  });
		  
	  } );
}

function checkStatus(obj,event) {
	if(event.type=='focus'){
		if(jQ(obj).hasClass("searchdefault")){
			jQ(obj).removeClass("searchdefault").addClass("searchonit");
			jQ(obj).val("");
		}
	}else if(event.type=='blur'){
		if(jQ(obj).hasClass("searchonit")){
			if(jQ.trim(jQ(obj).val()).length==0){
				jQ(obj).removeClass("searchonit").addClass("searchdefault");
				if(eval(jQ("#search_type_sel").val())==1)
					jQ(obj).val("输入搜索的真实姓名");
				else
					jQ(obj).val("输入搜索的订阅商家名");
			}
		}
	}
}

function searchUser(obj){
	var inputobj=jQ(obj).prev("input");
	var val=jQ.trim(inputobj.val());
	if(inputobj.hasClass("searchdefault"))
		val="";
	var tm= encodeURI(val);
	tm=encodeURI(tm);
	if(val.length==0){
		if(eval(jQ("#search_type_sel").val())==1)
			jQ("#search_info").empty().append("输入您要搜索的档期用户。");
		else
			jQ("#search_info").empty().append("输入您要搜索的订阅商家名。");
		return;
	}else{
		var p = /[A-Za-z0-9\u4e00-\u9fa5]{1,20}$/;
		if(!p.test(val)){
			jQ("#search_info").empty().append("搜索内容为2字节~20字节的中文、英文或数字").show();
			return;
		}else{
			var usertype=eval(jQ("#search_type_sel").val());
			jQ.ajax({
				type:"POST",
				url: "userAjax!searchUserByName.action",
				data:{
					nameInput:val,
					usertype:usertype
				},
				success:function(data){
					if(usertype==1)
						htmlSearchData(data);
					else if(usertype==2)
						htmlCCData(data);
				}
			});
		}
	}
}

function htmlCCData(data){
	if(jQ(data).find("User").length==0){
		jQ("#search-result-count span.count").empty().append(0)
		jQ("#search-result-count").show();
		jQ("#search-result-content").empty();
		return;
	}else{
		jQ("#search-result-count span.count").empty().append(jQ(data).find("User").length);
		jQ("#search-result-count").show();
	}
	var usertype=eval(jQ("#var_usertype").val());
	var html="";
	jQ(data).find("User").each(function(){
		var uid=eval(jQ(this).attr("uid"));
		var name=jQ(this).find("name").text();
		var desc=jQ(this).find("job").text();
		var _ulogo=jQ(this).find("ulogo").text();
		
		var isSubcribe=eval(jQ(this).find("isSubcribe").text());
		var subscribedcount=eval(jQ(this).find("subscribedcount").text());
		html+="<div class=\"cc\" uid="+uid+">";
		html+="<div class=\"content\">";
		html+="<div class=\"userlogo\" style=\"background:url('"+_ulogo+"')\"></div>";
		html+="<span class=\"name\">"+name+"</span>";
		html+="<span class=\"desc\">"+desc+"</span>";
		html+="</div>";
		html+="<div class=\"opt\">";
		if(usertype==1){
			if(isSubcribe)
				html+="<a class=\"unsubopt\"></a>";
			else
				html+="<a class=\"subopt\"></a>";
			html+="<div title='订阅人数'><span class='icon'></span><span class='count'>"+subscribedcount+"</span></div>";
			html+="<a class=\"viewopt\" href='"+jQ("#ctxVal").val()+"/myclip/user.html?"+uid+"'></a>";
		}else{
			html+="<a class=\"viewopt\" href='"+jQ("#ctxVal").val()+"/myclip/user.html?"+uid+"'></a>";
		}
		html+="</div>";
		html+="</div>";
	});
	jQ("#search-result-content").empty().append(html);
	jQ("#search-result-content a.unsubopt").unbind("click").bind("click",unsubscribeCurrent);
	jQ("#search-result-content a.subopt").unbind("click").bind("click",subscribeCurrent);
}

function subscribeCurrent(){
	var thisobj=jQ(this);
	var optobj=jQ(this).parent();
	var divobj=optobj.parent();
	var uid=eval(divobj.attr("uid"));
	jQ.ajax({
		type:"POST",
		url: "contactAjax!subscribeCC.action",
		data:{
			uid:uid
		},
		success:function(data){
			var dataObj=eval("("+data+")");//转换为json对象
			if(dataObj.result){
				thisobj.removeClass().addClass("unsubopt").unbind("click").bind("click",unsubscribeCurrent);
			}
		}
	});
}
function unsubscribeCurrent(){
	var thisobj=jQ(this);
	var optobj=jQ(this).parent();
	var divobj=optobj.parent();
	var nameobj=divobj.find("div.content span.name");
	var name=nameobj.text();
	var uid=eval(divobj.attr("uid"));
	jQ("#msg_box_opt a").show();
	invitingOptReturnBox(thisobj,"确定不再订阅 <font>"+name+"</font> ?");
	jQ("#msg_box_opt a.confirm").unbind("click").bind("click",function(){
		jQ.ajax({
			type:"POST",
			url: "contactAjax!unsubscribeCC.action",
			data:{
				uid:uid
			},
			success:function(data){
				var dataObj=eval("("+data+")");//转换为json对象
				if(dataObj.result){
					jQ("#msg_box").find("div.msg_note").animate({top: 200+'px'},500,function(){
						jQ("#msg_box").hide();
						thisobj.removeClass().addClass("subopt").unbind("click").bind("click",subscribeCurrent);
					});
					
				}
			}
		});
	});
	jQ("#msg_box_opt a.cancel").unbind("click").bind("click",function(){
		jQ("#msg_box").find("div.msg_note").animate({top: 200+'px'},500,function(){jQ("#msg_box").hide();});
	});
	
}

function invitingOptReturnBox(obj,content){
	var obj_box = jQ(obj);
	var obj_offset = obj_box.offset();
	var msg_box = jQ("#msg_box");
	jQ("#msg_box_content").empty().append(content);
	var _top = obj_offset.top - msg_box.outerHeight() +10;
	var _left = obj_offset.left - msg_box.outerHeight()/2;
	msg_box.css({"top":_top+"px","left":_left+"px"}).show();
	msg_box.find("div.msg_note").animate({top: 20+'px'}, 500,function(){
		//setTimeout(function(){
		//	
		//},2000);
	});
}


function htmlSearchData(data){
	if(jQ(data).find("User").length==0){
		jQ("#search-result-count span.count").empty().append(0)
		jQ("#search-result-count").show();
		jQ("#search-result-content").empty();
		return;
	}
	jQ("#search-result-count span.count").empty().append(jQ(data).find("User").length);
	jQ("#search-result-count").show();
	var usertype=eval(jQ("#var_usertype").val());
	var html="";
	jQ(data).find("User").each(function(){
		var uid=eval(jQ(this).attr("uid"));
		var name=jQ(this).find("name").text();
		var email=jQ(this).find("email").text();
		var _ulogo=jQ(this).find("ulogo").text();
		
		var isfriend=eval(jQ(this).find("isfriend").text());
		
		
		html+="<div class=\"element\">";
		html+="<div class=\"top\">";
		html+="<ul class=\"userinfo\">";
		html+="<li class=\"name\">"+name+"<a class='homepage' href='"+jQ("#ctxVal").val()+"/myclip/user.html?"+uid+"'></a></li>";
		html+="<li><span>邮箱：</span><span class=\"val\">"+email+"</span></li>";
		html+="<li class=\"btn\">";
		if(usertype==1){
			if(!isfriend){
				html+="<input type='hidden' class='uid' value='"+uid+"'/>";
				html+="<a class='addfriend'></a></li>";
			}else{
				html+="<a class='alreadyfriend'></a></li>";
			}
		}
		html+="</li>";
		html+="</ul>";
		html+="<div class=\"userlogo\" style=\"background:url('"+_ulogo+"')\">";
		html+="</div>";
		html+="</div>";
		html+="</div>";
	});
	jQ("#search-result-content").empty().append(html);
	jQ("a.addfriend").unbind("click").bind("click",addContact);
}

function getSex(val){
	if(val!=null&&val.length>0){
		var num=eval(val);
		if(num==1){
			return "男";
		}else if(num==2){
			return "女";
		}
	}else{
		return "";
	}
}

function formatBirthTime(daytime){
	if(daytime!=null){
		
		var temp=new Date();
		temp.setTime(eval(daytime));
		return temp.pattern("yyyy-MM-dd");
	}else
		return "";
}

//添加联系人
function addContact(event){
	jQ("a.addfriend").unbind("click");
	var optobj=event.target;
	jQ(optobj).removeClass("addfriend").addClass("loadingadd");
	var uid=jQ(optobj).prev().val();
	var type=1;
	var content="请求您加为好友";
	jQ.ajax({
		type:"POST",
		url: "contactAjax!addAsFriend.action",
		data:{
			id:uid,
			contactType:type,
			content:content
		},
		success:function(data){
			jQ(optobj).removeClass().addClass("alreadyfriend");
		}
	});
}
function getLength(str) { 
    var len = str.length; 
    var reLen = 0; 
    for (var i = 0; i < len; i++) {        
        if (str.charCodeAt(i) < 27 || str.charCodeAt(i) > 126) { 
            // 全角    
            reLen += 2; 
        } else { 
            reLen++; 
        } 
    } 
    return reLen;    
}
function subStr(str,length){
	var len=0;
	var restr="";
	for (var i = 0; i < str.length; i++) {
		if(len>length)
			break;
		else{
			if (str.charCodeAt(i) < 27 || str.charCodeAt(i) > 126) { 
				len += 2; 
			} else { 
				len++; 
			} 
			restr+=str.substr(i,1);
		}
	}
	return restr;
}

function changeUserType(obj){
	var val=eval(jQ(obj).val());
	if(val==1){
		jQ("#searchinput").val("输入搜索的真实姓名");
		jQ("#searchinput").removeClass().addClass("add_ul_input").addClass("searchdefault");
	}else{
		jQ("#searchinput").val("输入搜索的订阅商家名");
		jQ("#searchinput").removeClass().addClass("add_ul_input").addClass("searchdefault");
	}
	
}
